﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Configuration;
using System.Text;

public partial class CBOptionTrade : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            GridView1.Visible = false;
    }
    protected void queryBtn_Click(object sender, EventArgs e)
    {
        using (TR_DBDataContext db = new TR_DBDataContext())
        {
            // 取得ODS交易資料
            using (OracleConnection conn = new OracleConnection())
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString_ODS"].ConnectionString;
                conn.Open();
                OracleCommand cmd = conn.CreateCommand();
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT * FROM OD_OB_BONDOPT WHERE TRADESTATUS='VER' AND BOOK='OP_CB' ");
                sb.Append("AND COMPANY IN ('DBU', 'OBU') ");
                sb.Append("AND TRADEDATE=TO_DATE('" + reportDate.Text + "','yyyy/mm/dd')");
                string sqlStr = sb.ToString();
                cmd.CommandText = sqlStr;

                // Read oracle data
                OracleDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    DateTime tradeDate = reader["TRADEDATE"] != DBNull.Value ? (DateTime)reader["TRADEDATE"] : DateTime.Now;
                    string tradeID = reader["TRADEID"] != DBNull.Value ? ((string)reader["TRADEID"]).TrimEnd().ToString() : "";
                    var rptData = from P in db.TR_FormatA_TED where P.TradeDate.Date.Equals(tradeDate) && P.SequenceNo.Trim().Equals(tradeID) select P;

                    TR_FormatA_TED formatA;
                    if (rptData.Count() > 0)
                        formatA = (from P in db.TR_FormatA_TED where P.TradeDate.Date.Equals(tradeDate) && P.SequenceNo.Trim().Equals(tradeID) select P).First();
                    else
                        formatA = new TR_FormatA_TED();

                    // Table 欄位資料設定
                    formatA.ProductType = '4';
                    formatA.SequenceNo = reader["EXTERNALID"] != DBNull.Value ? ((string)reader["EXTERNALID"]).TrimEnd().ToString() : "";
                    formatA.BankCode = "8050000";
                    formatA.BankType = '1';
                    switch (reader["COMPANY"] != DBNull.Value ? ((string)reader["COMPANY"]).TrimEnd().ToString() : "")
                    {
                        case "DBU":
                            formatA.TransUnit = '1';
                            break;
                        case "OBU":
                            formatA.TransUnit = '2';
                            break;
                        case "HK":
                            formatA.TransUnit = '3';
                            break;
                        case "":
                            formatA.TransUnit = '0';
                            break;
                    }
                    //取得CustomerDefinition資料
                    Customer customer = new Customer();
                    customer.customerID = reader["CUST"] != DBNull.Value ? ((string)reader["CUST"]).TrimEnd().ToString() : "";
                    customer.getCustDataFromODS();
                    //取得ItemList資料
                    ItemList itemList = new ItemList();
                    itemList.itemCode = customer.groupID;
                    itemList.itemType = "CptyType";
                    itemList.getItemFromItemList();
                    formatA.CptyType = Convert.ToChar(itemList.itemName);
                    formatA.CptyName = customer.fullName;
                    switch (formatA.CptyType)
                    {
                        case '1':
                            BankCodeMaping bankMaping = new BankCodeMaping();
                            bankMaping.summitCustID = reader["CUST"] != DBNull.Value ? ((string)reader["CUST"]).TrimEnd().ToString() : "";
                            bankMaping.getBankCodeMapBySummitID();
                            formatA.CptyCode = bankMaping.fiscBankCode;
                            break;
                        case '2':
                        case '3':
                            formatA.CptyCode = customer.unino.Trim();
                            break;
                        case '4':
                        case '5':
                        case '6':
                        default:
                            formatA.CptyCode = "";
                            break;
                    }

                    formatA.TransReason = 'Z';
                    formatA.TradeDate = tradeDate;

                    formatA.EffectiveDate = reader["PREMDATA_DATE"] != DBNull.Value ? (DateTime)reader["PREMDATA_DATE"] : DateTime.MaxValue;
                    formatA.MaturityDate = reader["EXPDATE"] != DBNull.Value ? (DateTime)reader["EXPDATE"] : DateTime.MaxValue;
                    // 期初收付幣別
                    formatA.FirstPayRecCcy = "";
                    // 期初收付金額
                    formatA.FirstPayRecAmt = Convert.ToDecimal(0.00);
                    //名目本金幣別
                    formatA.NotionalCcy = reader["PREMDATA_CCY"] != DBNull.Value ? ((string)reader["PREMDATA_CCY"]).TrimEnd().ToString() : "";
                    //名目本金金額
                    formatA.NotionalAmt = reader["NOTIONAL"] != DBNull.Value ? Convert.ToDecimal(reader["NOTIONAL"]) : Convert.ToDecimal("0.00");
                    //標的資產類別
                    formatA.UnderlingType = '3';
                    //標的資產明細
                    formatA.UnderlingDetail = reader["SECID"] != DBNull.Value ? ((string)reader["SECID"]).TrimEnd().ToString() : "";
                    //遠期契約買賣別
                    formatA.LongTermType = 'Z';
                    //遠期契約履約價(成交價)
                    formatA.LongTermPrice = Convert.ToDecimal(0.00);
                    //遠期契約履約方式
                    formatA.LongTermMode = 'Z';
                    //選擇權契約型態
                    formatA.OptContractKind = 'Z';
                    switch (reader["PORC"] != DBNull.Value ? ((string)reader["PORC"]).TrimEnd().ToString() : "")
                    {
                        case "C":
                            formatA.OptContractKind = '1';
                            break;
                        case "P":
                            formatA.OptContractKind = '2';
                            break;
                    }
                    //選擇權契約買賣別
                    formatA.OptContractType = 'Z';
                    switch (reader["PORS"] != DBNull.Value ? ((string)reader["PORS"]).TrimEnd().ToString() : "")
                    {
                        case "P":
                            formatA.OptContractType = '1';
                            break;
                        case "S":
                            formatA.OptContractType = '2';
                            break;
                    }
                    //選擇權契約履約價(STRIKE)
                    formatA.OptContractPrice = reader["STRIKE"] != DBNull.Value ? Convert.ToDecimal(reader["STRIKE"]) : Convert.ToDecimal("0.00");
                    //選擇權類別
                    formatA.OptionStyle = 'Z';
                    switch (reader["STYLE"] != DBNull.Value ? ((string)reader["STYLE"]).TrimEnd().ToString() : "")
                    {
                        case "AMER":
                            formatA.OptionStyle = '1';
                            break;
                        case "EURO":
                            formatA.OptionStyle = '2';
                            break;
                    }
                    //選擇權履約方式
                    formatA.OptionSettleType = 'Z';
                    switch (reader["SETTLEDATE_STLMODE"] != DBNull.Value ? ((string)reader["SETTLEDATE_STLMODE"]).TrimEnd().ToString() : "")
                    {
                        case "CASH":
                            formatA.OptionStyle = '1';
                            break;
                        case "PHYS":
                            formatA.OptionStyle = '2';
                            break;
                    }
                    //交換契約交易方向
                    formatA.ExchangeTrnsType = char.MinValue;
                    //營業日原則
                    formatA.ExchangeRollType = char.MinValue;
                    //是否複利計算
                    formatA.isCompounding = char.MinValue;
                    //利息交割方式
                    formatA.RateSettleType = char.MinValue;
                    // 固定利率
                    formatA.FixRate = Convert.ToDecimal(0.00);
                    // 付息周期
                    formatA.FixPaySchedule = string.Empty;
                    // 計息天數
                    formatA.FixBasis = char.MinValue;

                    // 浮動利率-利率指標
                    formatA.FloatIndex = string.Empty;
                    // 浮動利率-利率指標天期
                    formatA.FloatIntTermType = string.Empty;
                    // 浮動利率-付息周期
                    formatA.FloatTerm = string.Empty;
                    // 浮動利率-利率加減碼
                    formatA.FloatSpread = Convert.ToDecimal(0.00);
                    // 浮動利率-利率指標重設周期
                    formatA.FloatResetType = string.Empty;
                    // 浮動利率-計息天數
                    formatA.FloatBasis = char.MinValue;
                    //股權收益收付週期
                    formatA.EquityPayCycle = char.MinValue;
                    //股權收益參與率
                    formatA.EquityParticipate = Convert.ToDecimal(0.0000);
                    
                    formatA.TradeType = "CBOption";

                    // 不輸出GroupID為27,O,I的資料
                    if (!(customer.groupID.ToUpper().Equals("27") || customer.groupID.ToUpper().Equals("O") || customer.groupID.ToUpper().Equals("I")))
                    {
                        if (rptData.Count() <= 0)
                            db.TR_FormatA_TED.InsertOnSubmit(formatA);
                        db.SubmitChanges();
                    }
                }// end of while()
            }// end of using()
        }// end of using()

        // 顯示報表資料
        displayMode();
    }
    private void displayMode()
    {
        GridView1.Visible = true;
    }
    protected void exportToCsv_Click(object sender, EventArgs e)
    {
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;
        
    }
}
